package sf.model.fields;

import java.util.HashMap;
import java.util.LinkedHashMap;

/***********************************************************************
 * Module:  ZoomField.java
 * Author:  dzelic05
 * Purpose: Defines the Class ZoomField
 ***********************************************************************/

/** @pdOid 2de39762-4590-4e6f-ade5-44a448b72ce3 */
public class ZoomFieldModel extends RegexConstrainedFieldModel {

	private String parentTableCode;
	/** polja na koja se ovaj ZoomField odnosi i njihove vrednosti */
	@SuppressWarnings("unchecked")
	private LinkedHashMap value = new LinkedHashMap();
	/** Nazivi tipova polja na koje ce se ovaj zoom odnositi */
	private LinkedHashMap<String, String> types = new LinkedHashMap<String, String>();
	private String primaryTargetField;
	/** Sirina JTextFielda */
	private int size;

	private String delimiter = "-";

	public ZoomFieldModel() {

	}

	public ZoomFieldModel(LinkedHashMap<String, String> types) {
		setTypes(types);
	}

	public String getParentTableCode() {
		return parentTableCode;
	}

	public void setParentTableCode(String parentTableCode) {
		this.parentTableCode = parentTableCode;
	}

	@Override
	public Object getValue() {
		// TODO Auto-generated method stub
		return value;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setValue(Object value) {
		// TODO Auto-generated method stub
		if (value instanceof String)
			setValueByString((String) value);
		else
			this.value = (LinkedHashMap) value;
		fireChanged();
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getPrimaryTargetField() {
		return primaryTargetField;
	}

	public void setPrimaryTargetField(String primaryTargetField) {
		this.primaryTargetField = primaryTargetField;
	}

	@SuppressWarnings("unchecked")
	public HashMap<String, Object> getFilterFields() {
		HashMap<String, Object> filter = (HashMap<String, Object>) getValue();

		filter.remove(getPrimaryTargetField());

		return filter;
	}

	public Object getPrimaryTargetFieldValue() {
		return value.get(primaryTargetField);
	}

	public String getValueString() {
		StringBuilder val = new StringBuilder();
		int n = value.values().size();
		int i = 0;
		for (Object s : value.values()) {
			String str = (String) s;
			val.append(str);
			if (i < n - 1)
				val.append(delimiter);
			i++;
		}
		return val.toString();
	}

	public void setValueByString(String val) {
		setValueByString(val, delimiter);
	}

	public void setValueByString(String s, String delimiter) {
		if (!check(s))
			return;
		String[] parts = s.split(delimiter);
		for (int i = 0; i < parts.length; i++) {
			// TODO Stao ovde...
		}
	}

	public LinkedHashMap<String, String> getTypes() {
		return types;
	}

	public void setTypes(LinkedHashMap<String, String> types) {
		this.types = types;
		String regex = TypeRegexConverter.getRegexForComposite(delimiter,
				this.types.values());
		setPatternByString(regex);
	}

	public void setDelimiter(String delimiter) {
		this.delimiter = delimiter;
		String regex = TypeRegexConverter.getRegexForComposite(delimiter,
				this.types.values());
		setPatternByString(regex);
	}
}